	### server side game wold
	### 
	
		
import GameLogic
#from objects import *
from random import *

from PodSixNet import rencode

import time

import starfield
from starfield import Player, Galaxy

cont = GameLogic.getCurrentController()
own = cont.owner
scene = GameLogic.getCurrentScene()
		
		
class GameMaster():
	
	def __init__(self):
		self.players = []
		self.gamestate = 0
		self.seed = randint(1,1000)
		self.stars = 10

		
		self.time_offset = time.time()
		self.time = time.time() - self.time_offset
		
		#this is the id index for every serializable game object
		#advantages to a unique id include reference in a global object listing
		self.id_index = 0
	
	def get_new_id(self):
		self.id_index += 1
		return self.id_index-1
		
	def CreateGalaxy(self):
		print("creating galaxy...")
		#watch this so old galaxies aren't fucking with new one
		self.id_index = 0
		starfield.server.SendToAll({"action": "message", "message": "Creating Galaxy of "+str(self.stars)+ " stars with seed "+str(self.seed)})
		starttime = time.time()
		
		starfield.galaxy = Galaxy.Galaxy( )
		self.galaxy = starfield.galaxy
		self.galaxy.generateStars(self.seed, self.stars)
		endtime = time.time()
		
		print("galaxy created: ", (endtime - starttime))
		print("number of stars:", len(self.galaxy.stars) )
		print("placing players...")
		starfield.server.SendToAll({"action": "message", "message": "Galaxy Created ["+str(endtime-starttime)+"seconds]"})
		
		starfield.server.SendToAll({"action": "message", "message": "Finding starting positions for "+str(len(self.players))+" players.."})
		starttime = time.time()	
		starting_locations = self.galaxy.placePlayers( len(self.players) )		
		endtime = time.time()
		print("players placed: ", (endtime - starttime))
		starfield.server.SendToAll({"action": "message", "message": "Players placed ["+str(endtime-starttime)+"seconds]"})
		
		##start the game clock
		#disregard the seconds before creation
		self.time_offset = time.time()
		self.time = time.time() - self.time_offset
		starfield.server.SendToAll({"action": "startclock"})
		
		self.send_all_stars()
		
		for entry in self.players:
			if len(starting_locations) > 0:
				entry.Place( starting_locations.pop(0))

		
		## check for initial ship update
		for entry in self.players:
			for star in entry.known_systems:
				entry.report_ships(star)		
			
		
	def Pump(self):
		self.time = time.time() - self.time_offset
		for entry in self.players:
			entry.Pump(self.time)
			
	def addPlayer(self, channel):
		newplayer = Player.Player(channel, self)
		self.players.append( newplayer )
		channel.avatar = newplayer
		
	def delPlayer(self, channel):
		for entry in self.players:
			if entry.channel == channel:
				self.players.remove( entry )
	
	def send_all_stars(self):
		packed = []
		for entry in starfield.galaxy.stars:
			packed.append( [entry.id, list(entry.location)] )
		for player in self.players:
			player.Send( {"action": "allstars", "stars":packed} )
